#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005;
const int Mod = 147744151;
const int P = 998244353;
int n, m, q, x1, y1, x2, y2, p, ans;
int a[N][N],sum[N][N];
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f*=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
inline int ksm(int a,int b){
	int cnt = 1;
	while(b){
		if(b&1) cnt = (cnt * a) % P;
		a = (a * a) % P;
		b >>= 1;
	} 
	return cnt % P;
} 
inline int Inv(int x){
	return ksm(x,P-2);
}
inline void sub1(){
	ans = 0;
	for(int t = 1; t <= q; t++){
		x1 = read(), y1 = read(), x2 = read(), y2 = read();
		int cnt = 1;
		for(int i = x1; i <= x2; i++){
			for(int j = y1; j <= y2; j++)
				cnt = (cnt * a[i][j]) % p;
		} 
		ans = (ans + (cnt ^ t)) % Mod;
	}
	cout << ans << endl;
} 
inline void sub2(){
	ans = 0;
	for(int i = 0; i <= m; i++) sum[0][i] = 1;
	for(int i = 0; i <= n; i++) sum[i][0] = 1;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			sum[i][j] = (sum[i - 1][j] * sum[i][j - 1] % P * Inv(sum[i - 1][j - 1]) % P * a[i][j]) % P;
	for(int t = 1; t <= q; t++){
		x1 = read(), y1 = read(), x2 = read(), y2 = read();
		int cnt = (sum[x2][y2] * Inv(sum[x1][y1]) % P * Inv(sum[x1 - 1][y2]) % P * Inv(sum[x2][y1 - 1]) % P * sum[x1][y1 - 1] % P * sum[x1 - 1][y1] % p * a[x1][y1]) % P;
		ans = (ans + (cnt ^ t)) % Mod;
	}
	cout << ans % Mod << endl;
}
signed main(){
	freopen("tsuki.in", "r", stdin);
	freopen("tsuki.out", "w", stdout);
	n = read(), m = read(), p = read();
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			a[i][j] = read();
	q = read();
	if(n <= 200 && m <= 200) sub1(); 
	else sub2();
	return 0;
}
/*
5 5 14
1 11 5 11 1
3 9 11 2 2
13 1 9 7 13
5 3 5 3 11
5 9 9 13 2
5
1 3 5 4
5 4 5 4
5 1 5 5
1 2 3 4
3 1 3 4
*/

